package cn.finedo.controller;

import cn.finedo.common.NonUtil;
import cn.finedo.common.ReturnValueDomain;
import cn.finedo.dto.AbilityDirDataDto;
import cn.finedo.dto.DirDataDto;
import cn.finedo.service.AbilityDataService;
import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
@RequestMapping("/dirdata")
@Scope("singleton")
public class AIAbilityDirDataController {

    @Autowired
    private AbilityDataService abilityDataService;

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 上传图片文件到服务器临时文件夹，并返回uuid
     * @param dto
     * @return
     */
    @ResponseBody
    @RequestMapping({"/uploadimages"})
    public ReturnValueDomain<AbilityDirDataDto> uploadAbilityMarkInfo(@RequestBody DirDataDto dto) {
        ReturnValueDomain<AbilityDirDataDto> ret = new ReturnValueDomain<>();
        List<String> imageBase64List = dto.getImageBase64List();
        List<String> fileNameList = dto.getFileNameList();
        if (NonUtil.isNon(dto) || NonUtil.isNon(imageBase64List) || NonUtil.isNon(fileNameList) || imageBase64List.size() != fileNameList.size()) {
            return ret.setFail("请检查必要参数");
        }
        return abilityDataService.uploadAbilityImage(dto);
    }

    /**
     * 根据aino查询能力对应的标签信息
     * @param dto
     * @return
     */
    @ResponseBody
    @RequestMapping({"/queryLabels"})
    public ReturnValueDomain<AbilityDirDataDto> uploadToMakeSence(@RequestBody AbilityDirDataDto dto) {
        ReturnValueDomain<AbilityDirDataDto> ret = new ReturnValueDomain<>();
        if (NonUtil.isNon(dto) || NonUtil.isNon(dto.getAino())) {
            return ret.setFail("缺少必要参数");
        }
        return abilityDataService.queryLabelsByAino(dto);
    }

    /**
     * 根据uuid 加载图片并渲染
     * @param dto
     * @return
     */
    @RequestMapping({"/loadimagebyid"})
    public void uploadTo(@RequestBody AbilityDirDataDto dto, HttpServletResponse response) {
        if (NonUtil.isNon(dto) || NonUtil.isNon(dto.getAino()) || NonUtil.isNon(dto.getTempDirUUID())) {
            logger.error("缺失必要参数");
            return;
        }
         abilityDataService.queryPictureToMakeSence(response, dto);
    }

    @ResponseBody
    @RequestMapping({"/savemarkinfo"})
    public ReturnValueDomain<String> saveMarkInfo(@RequestBody AbilityDirDataDto dto) {
        ReturnValueDomain<String> ret = new ReturnValueDomain<>();
        if (NonUtil.isNon(dto) || NonUtil.isNon(dto.getAino()) || NonUtil.isNon(dto.getTempDirUUID()) || NonUtil.isNon(dto.getPicresult())) {
            return ret.setFail("缺失必要参数");
        }
        return abilityDataService.savePicAndMarkInfo(dto);
    }

    

}
